package com.zzh.fullscreen;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 47606
 */

public class Bullet extends Flyer {

    protected static BufferedImage[] image;
    protected static BufferedImage[] img;
    final static int MAX = 8;

    static int[] width;
    static int[] height;
    int type = 0;

    static {

        try {
            image = new BufferedImage[MAX];
            img = new BufferedImage[MAX];

            width = new int[MAX];
            height = new int[MAX];

            for (int i = 0; i < MAX; i++) {
                image[i] = ImageIO.read(Bullet.class.getClassLoader()
                        .getResourceAsStream("img/my_bullet" + i + ".png"));
                System.out.println("my_bullet" + i + ".png");
                width[i] = image[i].getWidth();
                height[i] = image[i].getHeight();
                if (i > 2) {
                    width[i] = 50;
                    height[i] = 50;
                }
                // if(i>=2&&i<=4){
                // width[i]=50;
                // height[i]=50;
                // }else if(i>4){
                // width[i]=80;
                // height[i]=80;
                // }
            }
            for (int i = 0; i < MAX; i++) {
                img[i] = ImageOperate.rotateImage(image[i], 180);
            }
        } catch (IOException ioe) {
            System.out.println("载入子弹资源异常！");
            ioe.printStackTrace();
        }
    }

    /**
     * @param type 对应的资源顺序索引
     */
    public Bullet(int type, int x, int y, int vy) {
        super(x, y, 0, 0);
        speed = 5; // 6
        this.type = type;
        if (type == 1) {
            speed += 2;
        } else if (type == 2) {
            speed += 2;
        } else if (type == 3) {
            speed += 2;
        } else if (type == 4) {
            speed += 2;
        } else if (type == 7) {
            speed += 4;
        }
        if (vy > 0) {
            vely = speed;
        } else if (vy < 0) {
            vely = -speed;
        } else {
            vely = 0;
        }

        this.x -= width[type] / 2;
        cx = x - width[type] / 2;
        cy = y;
        cwidth = width[type];
        cheight = height[type];
        if (type == 3 || type == 4 || type == 6) {
            cx += 10;
            cwidth -= 20;
        }
        if (type == 5) {
            cx += 15;
            cwidth -= 35;
        }
    }

    public int getWidth() {
        return width[type];
    }

    public int getHeight() {
        return height[type];
    }

    public void paint(Graphics g, int f) {
        if (f == UP) {
            g.drawImage(image[type], this.x, this.y, width[type], height[type],
                    null);
//			g.drawLine(this.x+width[type]/2, 0,this.x+width[type]/2, 700);
            // g.drawRect(cx,cy,cwidth,cheight);

        } else {
            g.drawImage(img[type], this.x, this.y, width[type], height[type],
                    null);
//			 g.drawLine(this.x+width[type]/2, 0,this.x+width[type]/2, 700);
            // g.drawRect(cx,cy,cwidth,cheight);
        }
        //绘制碰撞矩形
        this.drawCollide(g);
    }
}
